
import { mgrs, sers } from "../../system/OverallSystem ";
import { GAME_MODE } from "../game/gameMode_Json";
import PageBase from "./pageBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameSettingPage extends PageBase {

    public pageName: string = "游戏设置";

    @property({ type: cc.SpriteFrame, displayName: "按钮图样" })
    private btnSf: cc.SpriteFrame[] = [];

    @property({ type: cc.Node, displayName: "背景音按钮" })
    private btnMusic: cc.Node = null;

    @property({ type: cc.Node, displayName: "音效按钮" })
    private btnSound: cc.Node = null;

    @property({ type: cc.Node, displayName: "震动按钮" })
    private btnVibration: cc.Node = null;


    @property({ type: cc.Node, displayName: "人声按钮" })
    private renshengBtn: cc.Node = null;

    @property({ type: cc.Node, displayName: "方言按钮" })
    private fangyanBtn: cc.Node = null;

    @property(cc.Node)
    BtnNode:cc.Node = null

    @property(cc.ToggleContainer)
    toggleG:cc.ToggleContainer = null

    onLoad() {

    }
    private number = 0
    gm(){
        this.number++
        if(this.number>20){
            window['GM-mode'] = true
        }
        if(this.number>30){
            localStorage.clear()
        }
        if(window['tt'] && window['tt'].setClipboardData){
            window['tt'].setClipboardData({
                data: window['user_id']+'',
                success(res) {
                    // sers().uiSrv.showMessage('复制ID成功')
                  console.log(`setClipboardData调用成功`);
                },
                fail(res) {
                    console.log(res)
                  console.log(`setClipboardData调用失败`);
                },
              });
        }
        
    }

    start() {
        this._init();
        let d = sers().userSrv.getSettingData()
        let check = d[5] || 0
        this.toggleG.node.children[check].getComponent(cc.Toggle).check()

        // if(this.node.getChildByName('content').getChildByName('user_id') && this.node.getChildByName('content').getChildByName('user_id').getComponent(cc.Label)){
        //     this.node.getChildByName('content').getChildByName('user_id').getComponent(cc.Label).string = 'user_id:'+window['user_id']

        //     this.node.getChildByName('content').getChildByName('user_id').on(cc.Node.EventType.TOUCH_START, ()=>{
        //         if(window['tt'] && window['tt'].setClipboardData){
        //             window['tt'].setClipboardData({
        //                 data: window['user_id']+'',
        //                 success(res) {
        //                     sers().uiSrv.showMessage('复制ID成功')
        //                   console.log(`setClipboardData调用成功`);
        //                 },
        //                 fail(res) {
        //                     console.log(res)
        //                   console.log(`setClipboardData调用失败`);
        //                 },
        //               });
        //         }
        //     }, this)
           
        // }
        
    }

    setCloseDoFun(colseDoFun: Function) {
        this.colseDoFun = colseDoFun;
    }

    protected _init(): void {
        this.updateSoundBan();
        this.updateMusicBan();
        this.updateVibration();
        this.updaterensheng();
        this.updateFangyan();

        this.open();
        // sers().platformSrv.platformApi.showBannerAD()
    }

    type = 0
    showBtn(t){
        this.BtnNode.active = t
        this.type = t
        // if(t==0){
        //     this.BtnNode.active = false
        // }else if(t==1){
        //     this.BtnNode.active = true
        //     this.BtnNode.children[0].active = false
        // }else{
        //     this.BtnNode.active = true
        // }
        if(t==0){
            this.BtnNode.active = false
        }else{
            this.BtnNode.active = true
        }
    }

    OnBtnSound() {
        mgrs().soundMgr.playBtnSound();
        mgrs().soundMgr.enableSound(!mgrs().soundMgr.isEnableSound);
        this.updateSoundBan();
    }

    OnBtnMusic() {
        mgrs().soundMgr.playBtnSound();
        mgrs().soundMgr.enableAudio(!mgrs().soundMgr.isEnableAudio);
        this.updateMusicBan();
    }

    OnBtnVibration() {
        mgrs().soundMgr.playBtnSound();
        mgrs().soundMgr.enableVibration(!mgrs().soundMgr.isVibration);
        console.log(mgrs().soundMgr.isVibration)
        this.updateVibration();
    }

    OnRensheng() {
        mgrs().soundMgr.playBtnSound();
        // window['rensheng'] = !window['rensheng']
        mgrs().soundMgr.enableRensheng(!mgrs().soundMgr.isRensheng);
        this.updaterensheng();
    }

    Onfangyan() {
        mgrs().soundMgr.playBtnSound();
        // window['rensheng'] = !window['rensheng']
        mgrs().soundMgr.enableFangyan(!mgrs().soundMgr.isFangyan);
        this.updateFangyan();
    }

    Toggle(e,d){
        let data = sers().userSrv.getSettingData()
        data[5] = +e.node.name
        sers().userSrv.setSettingData(data)
    }


    // OnBtnVibration() {
    //     this.updateVibrationBan();
    // }

    private updateSoundBan() {
        this.btnSound.getChildByName("ban").active = !mgrs().soundMgr.isEnableSound;
        this.btnSound.getChildByName("kai").active = mgrs().soundMgr.isEnableSound;
        
        // this.btnSound.getComponent(cc.Sprite).spriteFrame = this.btnSf[Number(mgrs().soundMgr.isEnableAudio)];
    }

    private updateMusicBan() {
        this.btnMusic.getChildByName("ban").active = !mgrs().soundMgr.isEnableAudio;
        this.btnMusic.getChildByName("kai").active = mgrs().soundMgr.isEnableAudio;

    }

    private updateVibration() {
        this.btnVibration.getChildByName("ban").active = !mgrs().soundMgr.isVibration;
        this.btnVibration.getChildByName("kai").active = mgrs().soundMgr.isVibration;

    }

    updaterensheng(){
        this.renshengBtn.getChildByName("ban").active = !mgrs().soundMgr.isRensheng;
        this.renshengBtn.getChildByName("kai").active =  mgrs().soundMgr.isRensheng;
    }

    updateFangyan(){
        this.fangyanBtn.getChildByName("ban").active = !mgrs().soundMgr.isFangyan;
        this.fangyanBtn.getChildByName("kai").active =  mgrs().soundMgr.isFangyan;
    }

    OnBtnBackIndex() {
        sers().platformSrv.platformApi.hideBanner()

        mgrs().soundMgr.playBtnSound();
        mgrs().gameMgr.backHomePage();
        // sers().uiSrv.showExit()
        this.OnBtnClose();

    }

    closePage(): void {
        this.OnBtnClose()
        sers().platformSrv.platformApi.FullScreenVideoAd()
    }

    chongzhiyouxi(){
        // sers().userSrv.setStoreData(null)
        // sers().userSrv.setStoreMajiang2Data({
        //     level:1
        // })

        // let data = sers().userSrv.getStoreData()

        // data.upNode = []
        // data.lock = []
        // data.downNode = []
        // data.gameTime = 0
        // data.nowLockPaiId = 2
        // sers().userSrv.setStoreData(data)
        

        this.closePage()

        if(this.type!=1 && this.type!=2){
            if(sers().userSrv.power<=0){
                sers().uiSrv.showUserGetPowerPage();
                return
            }
            sers().userSrv.addPower(-1)
        }

        cc.director.getPhysicsManager().enabled = false


        if(this.type==1){
            mgrs().gameMgr.enterMode(GAME_MODE.MAJIANGLEVEL);
        }else if(this.type==2){
            
            sers().userSrv.setStoreMajiang2Data({
                level:1
            })
            mgrs().gameMgr.enterMode(GAME_MODE.MAJIANG);
        }else if(this.type==3){
            mgrs().gameMgr.enterMode(GAME_MODE.MajiangGuohe);
        }else if(this.type==4){
            mgrs().gameMgr.enterMode(GAME_MODE.Paidui);
        }else if(this.type==5){
            mgrs().gameMgr.enterMode(GAME_MODE.Liushui);
        }else if(this.type==6){
            mgrs().gameMgr.enterMode(GAME_MODE.moveBrick);
        }else if(this.type==7){
            mgrs().gameMgr.enterMode(GAME_MODE.Peidui);
        }else if(this.type==8){
            mgrs().gameMgr.enterMode(GAME_MODE.Huahua);
        }else if(this.type==9){
            sers().userSrv.setDaxiguaMapData({
                level:2
            })
            mgrs().gameMgr.enterMode(GAME_MODE.HechengDaxigua);
        }else if(this.type==10){
            mgrs().gameMgr.enterMode(GAME_MODE.FanPai);
        }else if(this.type==11){
            sers().userSrv.setDaxiguaMapData2({
                level:2
            })
            mgrs().gameMgr.enterMode(GAME_MODE.HechengDaxigua2);
        }else if(this.type==12){
            mgrs().gameMgr.enterMode(GAME_MODE.Tianti);
        }else if(this.type==13){
            mgrs().gameMgr.enterMode(GAME_MODE.Paopaolong);
        }
        else if(this.type==14){
            sers().userSrv.setDaxiguaMapData3({
                level:2
            })
            mgrs().gameMgr.enterMode(GAME_MODE.HechengDaxigua3);
        }else if(this.type==15){
            
            let old = sers().userSrv.getStoreData()
            let l = 1
            if(old && old.level){
                l = old.level
            }
            let d = {
                level: l || 1,
                upNode: [],
                downNode: [],
            }
            sers().userSrv.setStoreData(d)
            mgrs().gameMgr.enterMode(GAME_MODE.PUKEPAI);
        }else if(this.type==16){
            sers().userSrv.setDaxiguaMapData4({
                level:2
            })
            mgrs().gameMgr.enterMode(GAME_MODE.HechengDaxigua4);
        }else if(this.type==17){
            let d = sers().userSrv.getFandabingMapData()
            d.upNode = []
            sers().userSrv.setFandabingMapData(d)
            mgrs().gameMgr.enterMode(GAME_MODE.Fandabing);
        }else if(this.type==18){
            mgrs().gameMgr.enterMode(GAME_MODE.Xianyu);
        }else if(this.type==19){
            mgrs().gameMgr.enterMode(GAME_MODE.Fangkuai);
        }
    }

    goIndex(){
        mgrs().gameMgr.backHomePage();
    }
    // update (dt) {}
}
